package com.hjx.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import com.hjx.service.userService;

public class LoginServlet extends HttpServlet {
    private String usename=null;
    private HttpSession session=null;
    private ServletContext context=null;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        context=req.getServletContext();

        session=req.getSession();
        boolean isLegal=false;
        userService user=new userService();
        String username=req.getParameter("user");
        String pass=req.getParameter("pass");
        System.out.println("user:"+username+"  "+"pass:"+pass);
        try {
            //返回用户的名字
            if(username!=null && username!="" && pass!=null && pass!=""){
                isLegal=user.validLogin(username,pass);
            }else{
                System.out.println("用户输入数据为空");
            }
        } catch (Exception e) {
            e.printStackTrace();

        }
//            service层对密码已经判断了，只不过返回的是一个用户名
            if (isLegal){
//                将用户信息存入servletContext中  存入session中的话，在过滤器中不知怎么获取session对象
                session.setAttribute("user",username);
                System.out.println(username+"登录成功");
                resp.sendRedirect("/super/Page/index.jsp");
            }else{
                System.out.println("登录失败！！");
                resp.sendRedirect("/super/LoginValidate/loginFail.jsp");
            }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}
